package com.game.logic.service;

import com.game.framework.common.utils.StringUtil;
import com.game.framework.dao.ActivityInfoDao;
import com.game.framework.dao.entity.*;
import com.game.framework.dao.mapper.*;
import com.game.framework.message.protobuf.ActivityProto;
import com.game.framework.message.protobuf.BaseProto;
import com.game.logic.service.rank.RankInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    protected ActivityFishBaitMapper activityFishBaitMapper;
    @Autowired
    protected ActivityFishPoleMapper activityFishPoleMapper;
    @Autowired
    protected ActivityFishTypeMapper activityFishTypeMapper;
    @Autowired
    protected ActivityObjectMapper activityObjectMapper;
    @Autowired
    protected ActivityTicketMapper activityTicketMapper;
    @Autowired
    protected ActivityPlayerMapper activityPlayerMapper;
    @Autowired
    protected ActivityRankMapper activityRankMapper;
    @Autowired
    protected ActivityMapper activityMapper;
    @Autowired
    protected FishPoleMapper fishPoleMapper;
    @Autowired
    protected UserInfoMapper userInfoMapper;
    @Autowired
    protected RoleMapper roleMapper;
    @Autowired
    protected ActivityInfoDao activityInfoDao;

    public ActivityProto.ActivityInfo.Builder generateActivityInfo(ActivityEntity activityEntity) {
        ActivityProto.ActivityInfo.Builder activityInfo = ActivityProto.ActivityInfo.newBuilder();
        activityInfo.setId(activityEntity.getId());
        activityInfo.setName(activityEntity.getName());
        activityInfo.setLimitPlayer(activityEntity.getLimitPlayer());
//        activityInfo.setLimitTicketType(activityEntity.getLimitTicketType());
//        activityInfo.setLimitTicketNum(activityEntity.getLimitTicketNum());
//        activityInfo.setLimitPoleType(activityEntity.getLimitPoleType());
//        activityInfo.setLimitPoleNum(activityEntity.getLimitPoleNum());
//        activityInfo.setLimitDragonType(activityEntity.getLimitDragonType());
//        activityInfo.setLimitDragonNum(activityEntity.getLimitDragonNum());
//        activityInfo.setLimitObjects(activityEntity.getLimitObjects());
        activityInfo.setCurPlayerNum(activityEntity.getCurPlayerNum());
        activityInfo.setMaxPlayerNum(activityEntity.getMaxPlayerNum());
        activityInfo.setStatus(activityEntity.getStatus());
        if(ObjectUtils.isNotEmpty(activityEntity.getStartTime())) {
            activityInfo.setStartTime(activityEntity.getStartTime().getTime());
        }
        if(ObjectUtils.isNotEmpty(activityEntity.getEndTime())) {
            activityInfo.setEndTime(activityEntity.getEndTime().getTime());
        }
        return activityInfo;
    }

    public ActivityProto.FishInfo.Builder generateActivityFishTypeInfo(ActivityFishTypeEntity activityFishTypeEntity) {
        return ActivityProto.FishInfo.newBuilder()
                .setFishType(activityFishTypeEntity.getFishType())
                .setFishName(activityFishTypeEntity.getFishName())
                .setFishCount(activityFishTypeEntity.getFishCount())
                .setRemainCount(activityFishTypeEntity.getRemainCount());
    }

    @Override
    public BaseProto.ErrorCode getActivityList(ActivityProto.GetActivityListRequest request, ActivityProto.GetActivityListResponse.Builder response) {
        //RoleEntity roleEntity = roleMapper.selectByRoleId(request.getRoleId());
        List<ActivityEntity> activityEntityList = activityMapper.selectAll();
        if(CollectionUtils.isNotEmpty(activityEntityList)){
            for(ActivityEntity activityEntity : activityEntityList){
                ActivityProto.ActivityInfo.Builder activityInfo = generateActivityInfo(activityEntity);
                //活动投放鱼信息
                List<ActivityProto.FishInfo> fishInfoList = new ArrayList<>();
                List<ActivityFishTypeEntity> activityFishTypeEntityList = activityFishTypeMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityFishTypeEntityList)){
                    for (ActivityFishTypeEntity activityFishTypeEntity : activityFishTypeEntityList){
                        ActivityProto.FishInfo.Builder fishInfo = generateActivityFishTypeInfo(activityFishTypeEntity);
                        fishInfoList.add(fishInfo.build());
                    }
                }
                if(CollectionUtils.isNotEmpty(fishInfoList)) {
                    activityInfo.addAllFishInfos(fishInfoList);
                }
                //活动限制门票令牌信息
                List<ActivityProto.TicketInfo> ticketInfoList = new ArrayList<>();
                List<ActivityTicketEntity> activityTicketEntityList = activityTicketMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityTicketEntityList)){
                    for (ActivityTicketEntity activityTicketEntity : activityTicketEntityList){
                        ActivityProto.TicketInfo.Builder ticketInfo = ActivityProto.TicketInfo.newBuilder()
                                .setTicketType(activityTicketEntity.getTicketType())
                                .setTicketNum(activityTicketEntity.getNumber());
                        ticketInfoList.add(ticketInfo.build());
                    }
                }
                if(CollectionUtils.isNotEmpty(ticketInfoList)) {
                    activityInfo.addAllTicketInfo(ticketInfoList);
                }
                //活动限制鱼饵信息
                List<ActivityProto.BaitInfo> baitInfoList = new ArrayList<>();
                List<ActivityFishBaitEntity> activityFishBaitEntityList = activityFishBaitMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityFishBaitEntityList)){
                    for (ActivityFishBaitEntity activityFishBaitEntity : activityFishBaitEntityList){
                        ActivityProto.BaitInfo.Builder baitInfo = ActivityProto.BaitInfo.newBuilder()
                                .setBaitType(activityFishBaitEntity.getBaitType())
                                .setBaitNum(activityFishBaitEntity.getNumber());
                        baitInfoList.add(baitInfo.build());
                    }
                }
                if(CollectionUtils.isNotEmpty(baitInfoList)) {
                    activityInfo.addAllBaitInfo(baitInfoList);
                }
                //活动限制藏品信息
                List<ActivityProto.ObjectInfo> objectInfoList = new ArrayList<>();
                List<ActivityObjectEntity> activityObjectEntityList = activityObjectMapper.selectListByActivityId(activityEntity.getId());
                if(CollectionUtils.isNotEmpty(activityObjectEntityList)){
                    for (ActivityObjectEntity activityObjectEntity : activityObjectEntityList){
                        ActivityProto.ObjectInfo.Builder objectInfo = ActivityProto.ObjectInfo.newBuilder()
                                .setObjectId(activityObjectEntity.getObjectId())
                                .setObjectName(activityObjectEntity.getObjectName())
                                .setObjectNum(activityObjectEntity.getNumber());
                        objectInfoList.add(objectInfo.build());
                    }
                }
                if(CollectionUtils.isNotEmpty(objectInfoList)) {
                    activityInfo.addAllObjectInfo(objectInfoList);
                }
                //TODO 暂时把限制条件去掉
//                if(activityEntity.getStatus() == 1) {
//                    if ((activityEntity.getLimitTicketNum() > roleEntity.getTicket())
//                            ||(activityEntity.getLimitDragon() > roleEntity.getRoleId())
//                            ||(activityEntity.getLimitPole() > roleEntity.getPole())) {
//                        activityEntity.setStatus(3);
//                    }
//                }


                response.addActivityInfos(activityInfo);
            }
        }
        return BaseProto.ErrorCode.SUCCESS;
    }

    @Override
    public BaseProto.ErrorCode getFishPoleList(ActivityProto.GetFishPoleListRequest request, ActivityProto.GetFishPoleListResponse.Builder response) {
        List<FishPoleEntity> fishPoleEntities = fishPoleMapper.selectAll();
        if(CollectionUtils.isNotEmpty(fishPoleEntities)){
            for(FishPoleEntity fishPoleEntity : fishPoleEntities) {
                ActivityProto.FishPoleInfo.Builder fishPoleInfo = ActivityProto.FishPoleInfo.newBuilder()
                        .setId(fishPoleEntity.getId())
                        .setName(fishPoleEntity.getName())
                        .setMinRate(fishPoleEntity.getMinRate())
                        .setMaxRate(fishPoleEntity.getMaxRate())
                        .setFishType(fishPoleEntity.getFishType());
                response.addFishPoleInfos(fishPoleInfo.build());
            }
        }
        return BaseProto.ErrorCode.SUCCESS;
    }

    public BaseProto.ErrorCode enterActivity(ActivityProto.EnterActivityRequest request, ActivityProto.EnterActivityResponse.Builder response) {
        Optional<ActivityInfo> activityInfoData = activityInfoDao.selectActivityInfoById(request.getActivityId());
        if(activityInfoData.isPresent()) {
            ActivityInfo activityInfo = activityInfoData.get();
            if(activityInfo.getPlayerMap().size() == activityInfo.getActivityEntity().getLimitPlayer()){
                return BaseProto.ErrorCode.ACTIVITY_ROLE_IS_FULL;
            }
            response.setActivityInfo(generateActivityInfo(activityInfo.getActivityEntity()));
            Map<Integer, ActivityFishTypeEntity> activityFishTypeEntityMap = activityInfo.getFishTypeMap();
            if(activityFishTypeEntityMap != null && activityFishTypeEntityMap.size() > 0){
                List<ActivityProto.FishInfo> fishInfoList = new ArrayList<>();
                for (ActivityFishTypeEntity activityFishTypeEntity : activityFishTypeEntityMap.values()){
                    ActivityProto.FishInfo.Builder fishInfo = generateActivityFishTypeInfo(activityFishTypeEntity);
                    fishInfoList.add(fishInfo.build());
                }
                response.addAllFishInfos(fishInfoList);
            }
            activityInfo.getPlayerMap().put(request.getRoleId(), 0);
            activityInfoDao.saveOrUpdate(activityInfo, activityInfo.getId());
        }
        return BaseProto.ErrorCode.SUCCESS;
    }

    @Override
    public BaseProto.ErrorCode fishingCastAction(ActivityProto.FishingCastActionRequest request, ActivityProto.FishingCastActionResponse.Builder response) {
        RoleEntity roleEntity = roleMapper.selectByRoleId(request.getRoleId());
        if(roleEntity == null) {
            return BaseProto.ErrorCode.PLAYER_NOT_EXIST;
        }
        Optional<ActivityInfo> activityInfoData = activityInfoDao.selectActivityInfoById(request.getActivityId());
        if(activityInfoData.isPresent()) {
            ActivityInfo activityInfo = activityInfoData.get();
            if(!activityInfo.getPlayerMap().containsKey(request.getRoleId())){
                return BaseProto.ErrorCode.ACTIVITY_ROLE_NOT_EXIST;
            }
            Map<Integer, ActivityFishTypeEntity> activityFishTypeEntityMap = activityInfo.getFishTypeMap();
            if (activityFishTypeEntityMap != null && activityFishTypeEntityMap.size() > 0) {
                Map<Integer, ActivityFishTypeEntity> fishTypeInfoList = new HashMap<>();
                for (ActivityFishTypeEntity activityFishTypeEntity : activityFishTypeEntityMap.values()) {
                    if (activityFishTypeEntity.getRemainCount() < activityFishTypeEntity.getFishCount()) {
                        fishTypeInfoList.put(activityFishTypeEntity.getFishType(), activityFishTypeEntity);
                    }
                }
                if (fishTypeInfoList.size() == 0) {
                    return BaseProto.ErrorCode.ACTIVITY_FISH_NOT_EXIST;
                }
                List<ActivityFishBaitEntity> activityFishBaitEntityList = activityFishBaitMapper.selectListByActivityId(request.getActivityId());
                if (CollectionUtils.isNotEmpty(activityFishBaitEntityList)) {
                    boolean isSuccess = false;
                    for (ActivityFishBaitEntity fishBaitEntity : activityFishBaitEntityList) {
                        Map<Integer, Integer> baitMap = StringUtil.stringToMapByValueInteger(roleEntity.getBait(), ";", ",");
                        if (baitMap.containsKey(fishBaitEntity.getBaitType())) {
                            Integer baitCount = baitMap.get(fishBaitEntity.getBaitType());
                            if (baitCount > 0) {
                                baitCount = baitCount - 1;
                                baitMap.put(fishBaitEntity.getBaitType(), baitCount);
                                String baitStr = StringUtil.Map2String(baitMap,";",",");
                                roleEntity.setBait(baitStr);
                                roleMapper.updateById(roleEntity);
                                isSuccess = true;
                                break;
                            }
                        }
                    }
                    if (!isSuccess) {
                        return BaseProto.ErrorCode.ACTIVITY_BAIT_NOT_EXIST;
                    }
                }
                FishPoleEntity fishPoleEntity = fishPoleMapper.selectById(request.getPoleId());
                int rateFishPole = RandomUtils.nextInt(fishPoleEntity.getMinRate(), fishPoleEntity.getMaxRate());
                if (rateFishPole < RandomUtils.nextInt(0, 100)) {
                    String[] fishTypes = fishPoleEntity.getFishType().split(",");
                    if (fishTypes.length > 0) {
                        int fishType = Integer.parseInt(fishTypes[RandomUtils.nextInt(0, fishTypes.length)]);
                        //把活动里面的剩余的鱼种的活鱼数量减 1
                        ActivityFishTypeEntity activityFishTypeEntity = activityFishTypeEntityMap.get(fishType);
                        activityFishTypeEntity.setRemainCount(activityFishTypeEntity.getRemainCount()-1);
                        activityFishTypeEntityMap.put(fishType, activityFishTypeEntity);
                        activityInfo.getPlayerMap().put(request.getRoleId(), activityInfo.getPlayerMap().get(request.getRoleId())+1);
                        activityInfoDao.saveOrUpdate(activityInfo, activityInfo.getId());
                        //把这次调到的鱼存入玩家活动钓鱼记录里面
                        ActivityPlayerEntity activityPlayerEntity = new ActivityPlayerEntity();
                        activityPlayerEntity.setActivityId(request.getActivityId());
                        activityPlayerEntity.setUserId(request.getUserId());
                        activityPlayerEntity.setRoleId(request.getRoleId());
                        activityPlayerEntity.setFishType(fishType);
                        activityPlayerEntity.setFishCount(1);
                        activityPlayerEntity.setFishPoleId(request.getPoleId());
                        activityPlayerEntity.setFishTime(new Date());
                        activityPlayerMapper.insert(activityPlayerEntity);
                        response.setActivityId(request.getActivityId());
                        response.setResult(1);
                        return BaseProto.ErrorCode.SUCCESS;
                    }
                } else {
                    response.setActivityId(request.getActivityId());
                    response.setResult(0);
                    return BaseProto.ErrorCode.SUCCESS;
                }
            }
        }
        return BaseProto.ErrorCode.UNKNOWN_ERROR;
    }

    @Override
    public BaseProto.ErrorCode fishingTakeUpAction(ActivityProto.FishingTakeupActionRequest request, ActivityProto.FishingTakeupActionResponse.Builder response) {
        RoleEntity roleEntity = roleMapper.selectByRoleId(request.getRoleId());
        if(roleEntity == null) {
            return BaseProto.ErrorCode.PLAYER_NOT_EXIST;
        }
        Optional<ActivityInfo> activityInfoData = activityInfoDao.selectActivityInfoById(request.getActivityId());
        if(activityInfoData.isPresent()) {
            ActivityInfo activityInfo = activityInfoData.get();
            if(!activityInfo.getPlayerMap().containsKey(request.getRoleId())){
                return BaseProto.ErrorCode.ACTIVITY_ROLE_NOT_EXIST;
            }
            Map<String, Object>sqlMap = new HashMap<>();
            sqlMap.put("activityId",request.getActivityId());
            sqlMap.put("userId",request.getUserId());
            sqlMap.put("roleId",request.getRoleId());
            sqlMap.put("fishPoleId",request.getPoleId());
            List<ActivityPlayerEntity> activityPlayerEntityList = activityPlayerMapper.selectByMap(sqlMap);
            if(CollectionUtils.isEmpty(activityPlayerEntityList)){
                response.setActivityId(request.getActivityId());
                response.setResult(0);
                response.setFishType(0);
                response.setFishCount(1);
                return BaseProto.ErrorCode.SUCCESS;
            }
            ActivityPlayerEntity activityPlayerEntity = activityPlayerEntityList.get(0);
            int fishType = activityPlayerEntity.getFishType();
            if(System.currentTimeMillis() - activityPlayerEntity.getFishTime().getTime() > 60000){
                //跑鱼
                Map<Integer, ActivityFishTypeEntity> activityFishTypeEntityMap = activityInfo.getFishTypeMap();
                if (activityFishTypeEntityMap != null && activityFishTypeEntityMap.size() > 0) {
                    //把活动里面的剩余的鱼种的活鱼数量加 1
                    ActivityFishTypeEntity activityFishTypeEntity = activityFishTypeEntityMap.get(fishType);
                    activityFishTypeEntity.setRemainCount(activityFishTypeEntity.getRemainCount() + 1);
                    activityFishTypeEntityMap.put(activityPlayerEntity.getFishType(), activityFishTypeEntity);
                    activityInfo.getPlayerMap().put(activityPlayerEntity.getRoleId(), activityInfo.getPlayerMap().get(
                            activityPlayerEntity.getRoleId()) - 1);
                    activityInfoDao.saveOrUpdate(activityInfo, activityInfo.getId());
                }
                activityPlayerMapper.deleteById(activityPlayerEntity.getId());
                response.setActivityId(request.getActivityId());
                response.setResult(0);
                response.setFishType(0);
                response.setFishCount(1);
                return BaseProto.ErrorCode.SUCCESS;
            }else{
                response.setActivityId(request.getActivityId());
                response.setResult(1);
                response.setFishType(fishType);
                response.setFishCount(1);
                String fish = roleEntity.getFish();
                if(StringUtils.isNotEmpty(fish)) {
                    Map<Integer, Integer> fishMap = StringUtil.stringToMapByValueInteger(fish, ";", ",");
                    if(fishMap.containsKey(fishType)){
                        fishMap.put(fishType, fishMap.get(fishType)+1);
                    }else {
                        fishMap.put(fishType, 1);
                    }
                    fish = StringUtil.mapToString(fishMap,";", ",");
                }else{
                    Map<Integer, Integer> fishMap = new HashMap<>();
                    fishMap.put(fishType,1);
                    fish = StringUtil.mapToString(fishMap,";", ",");
                }
                roleEntity.setFish(fish);
                roleMapper.updateById(roleEntity);
                return BaseProto.ErrorCode.SUCCESS;
            }
        }

        return BaseProto.ErrorCode.UNKNOWN_ERROR;
    }

    @Override
    public BaseProto.ErrorCode exitActivity(ActivityProto.ExitActivityRequest request, ActivityProto.ExitActivityResponse.Builder response) {
        Optional<ActivityInfo> activityInfoData = activityInfoDao.selectActivityInfoById(request.getActivityId());
        if(activityInfoData.isPresent()) {
            ActivityInfo activityInfo = activityInfoData.get();
            activityInfo.getPlayerMap().remove(request.getRoleId());
        }
        response.setActivityId(request.getActivityId());
        response.setResult(1);
        return BaseProto.ErrorCode.SUCCESS;
    }

    public BaseProto.ErrorCode getActivityRank(ActivityProto.GetActivityRankRequest request, ActivityProto.GetActivityRankResponse.Builder response) {
        RoleEntity roleEntity = roleMapper.selectByRoleId(request.getRoleId());
        if(roleEntity == null) {
            return BaseProto.ErrorCode.PLAYER_NOT_EXIST;
        }
        ActivityEntity activityDB = activityMapper.selectById(request.getActivityId());
        if(activityDB != null && activityDB.getStatus() == 1){
            Optional<ActivityInfo> activityInfoData = activityInfoDao.selectActivityInfoById(request.getActivityId());
            if(activityInfoData.isPresent()) {
                ActivityInfo activityInfo = activityInfoData.get();
                if (!activityInfo.getPlayerMap().containsKey(request.getRoleId())) {
                    return BaseProto.ErrorCode.ACTIVITY_ROLE_NOT_EXIST;
                }
                ActivityEntity activityEntity = activityInfo.getActivityEntity();
                response.setActivityId(activityEntity.getId());
                response.setActivityName(activityEntity.getName());
                response.setStartTime(activityEntity.getStartTime().getTime());
                response.setEndTime(activityEntity.getEndTime().getTime());
                response.setStatus(activityEntity.getStatus());
                response.setUserId(request.getUserId());
                response.setRoleId(request.getRoleId());

                Map<Long, Integer> playerMap = activityInfo.getPlayerMap();
                if (ObjectUtils.isNotEmpty(playerMap)) {
                    TreeSet<RankInfo> treeSet = new TreeSet<>((a, b) -> (int) (b.getAllCount() - a.getAllCount()));

                    for (Long roleId : playerMap.keySet()) {
                        RankInfo rankInfo = new RankInfo();
                        RoleEntity roleInfo = roleMapper.selectByRoleId(roleId);
                        UserInfoEntity userInfoEntity = userInfoMapper.selectByUserCode((int) roleInfo.getUserId());
                        rankInfo.setUserId(roleInfo.getUserId());
                        rankInfo.setRoleId(roleInfo.getRoleId());
                        rankInfo.setAccount(userInfoEntity.getPhone());
                        rankInfo.setPhone(userInfoEntity.getPhone());
                        rankInfo.setNickName(userInfoEntity.getNickName());
                        rankInfo.setHeadImg(userInfoEntity.getHeadimgUrl());
                        rankInfo.setAllCount(playerMap.get(roleId));
                        treeSet.add(rankInfo);
                    }

                    List<ActivityProto.RankInfo> rankInfoList = new ArrayList<>();
                    int index = 0;
                    for (RankInfo rank : treeSet) {
                        index++;
                        if (request.getRoleId() == rank.getRoleId()) {
                            response.setMyIndex(index);
                        }
                        ActivityProto.RankInfo rankInfo = ActivityProto.RankInfo.newBuilder()
                                .setIndex(index)
                                .setUserId(rank.getUserId())
                                .setRoleId(rank.getRoleId())
                                .setAccount(rank.getAccount())
                                .setPhone(rank.getPhone())
                                .setNickName(rank.getNickName())
                                .setHeadImg(rank.getHeadImg())
                                .setCount(rank.getAllCount())
                                .build();
                        rankInfoList.add(rankInfo);
                        log.info("rankInfo :{}", rankInfo);
                    }
                    response.addAllRankInfos(rankInfoList);
                }
            }
        }else {
            Map<String, Object>sqlMap = new HashMap<>();
            sqlMap.put("activityId",request.getActivityId());
            sqlMap.put("userId",request.getUserId());
            sqlMap.put("roleId",request.getRoleId());
            List<ActivityRankEntity> activityRankEntityList = activityRankMapper.selectByMap(sqlMap);
            if(CollectionUtils.isNotEmpty(activityRankEntityList)) {
                List<ActivityProto.RankInfo> rankInfoList = new ArrayList<>();
                int index = 0;
                for (ActivityRankEntity rankEntity : activityRankEntityList) {
                    index++;
                    if (request.getRoleId() == rankEntity.getRoleId()) {
                        response.setMyIndex(index);
                    }
                    RoleEntity role = roleMapper.selectByRoleId(rankEntity.getRoleId());
                    ActivityProto.RankInfo rankInfo = ActivityProto.RankInfo.newBuilder()
                            .setIndex(index)
                            .setUserId(rankEntity.getUserId())
                            .setRoleId(rankEntity.getRoleId())
                            .setAccount(rankEntity.getPhone())
                            .setPhone(rankEntity.getPhone())
                            .setNickName(role.getNickName())
                            .setHeadImg(role.getHeadImg())
                            .setCount(rankEntity.getAllCount())
                            .build();
                    rankInfoList.add(rankInfo);
                    log.info("rankInfo :{}", rankInfo);
                }
                response.addAllRankInfos(rankInfoList);
            }
        }
        return BaseProto.ErrorCode.SUCCESS;
    }

    @Override
    public void ActivityScheduleLoad() {
        log.info("ActivityScheduleLoad!!!");
        List<ActivityEntity> activityEntityList = activityMapper.selectAll();
        if(CollectionUtils.isNotEmpty(activityEntityList)){
            for(ActivityEntity activityEntity : activityEntityList){
                if((System.currentTimeMillis() > activityEntity.getStartTime().getTime())
                        &&(System.currentTimeMillis() < activityEntity.getEndTime().getTime())){
                    Optional<ActivityInfo> activityInfoData = activityInfoDao.selectActivityInfoById(activityEntity.getId());
                    if(!activityInfoData.isPresent()) {
                        activityEntity.setStatus(1);
                        ActivityInfo activityInfo = new ActivityInfo();
                        activityInfo.setId(activityEntity.getId());
                        activityInfo.setActivityEntity(activityEntity);
                        List<ActivityFishTypeEntity>  activityFishTypeEntityList = activityFishTypeMapper.selectListByActivityId(activityEntity.getId());
                        if(CollectionUtils.isNotEmpty(activityFishTypeEntityList)) {
                            for (ActivityFishTypeEntity activityFishTypeEntity : activityFishTypeEntityList) {
                                activityInfo.getFishTypeMap().put(activityFishTypeEntity.getFishType(), activityFishTypeEntity);
                            }
                        }
                        activityInfoDao.saveOrUpdate(activityInfo, activityEntity.getId());
                        activityMapper.updateById(activityEntity);
                    }
                }
            }
        }
    }

    @Override
    public void ActivityScheduleUnload() {
        log.info("ActivityScheduleUnload!!!");
        List<ActivityInfo> activityInfoDataList = activityInfoDao.selectAllActivityInfo();
        if(CollectionUtils.isNotEmpty(activityInfoDataList)) {
            for(ActivityInfo activityInfo :activityInfoDataList){
                ActivityEntity activityEntity = activityInfo.getActivityEntity();
                if(System.currentTimeMillis() > activityEntity.getEndTime().getTime()){
                    activityEntity.setStatus(-1);
                    activityMapper.updateById(activityEntity);
                    Map<Integer, ActivityFishTypeEntity> fishTypeMap = activityInfo.getFishTypeMap();
                    for(ActivityFishTypeEntity activityFishTypeEntity : fishTypeMap.values()){
                        activityFishTypeMapper.updateById(activityFishTypeEntity);
                    }
                    activityInfoDao.saveOrUpdate(activityInfo, activityInfo.getId());
                    Map<Long, Integer> playerMap = activityInfo.getPlayerMap();
                    if(ObjectUtils.isNotEmpty(playerMap)) {
                        TreeSet<ActivityRankEntity> treeSet = new TreeSet<>((a, b) -> (int) (b.getAllCount() - a.getAllCount()));
                        for (Long roleId : playerMap.keySet()) {
                            ActivityRankEntity activityRankEntity = new ActivityRankEntity();
                            activityRankEntity.setActivityId(activityEntity.getId());
                            RoleEntity roleInfo = roleMapper.selectByRoleId(roleId);
                            activityRankEntity.setUserId(roleInfo.getUserId());
                            activityRankEntity.setRoleId(roleId);
                            UserInfoEntity userInfoEntity = userInfoMapper.selectByUserCode((int)roleInfo.getUserId());
                            activityRankEntity.setPhone(userInfoEntity.getPhone());
                            activityRankEntity.setAllCount(playerMap.get(roleId));
                            Map<String, Object> sqlMap = new HashMap<>();
                            sqlMap.put("activityId",activityEntity.getId());
                            sqlMap.put("roleId",roleId);
                            sqlMap.put("fishType",1);
                            int fishCount1 = activityPlayerMapper.selectCountByMap(sqlMap);
                            activityRankEntity.setFishCount1(fishCount1);
                            sqlMap.put("fishType",2);
                            int fishCount2 = activityPlayerMapper.selectCountByMap(sqlMap);
                            activityRankEntity.setFishCount2(fishCount2);
                            sqlMap.put("fishType",3);
                            int fishCount3 = activityPlayerMapper.selectCountByMap(sqlMap);
                            activityRankEntity.setFishCount3(fishCount3);
                            sqlMap.put("fishType",4);
                            int fishCount4 = activityPlayerMapper.selectCountByMap(sqlMap);
                            activityRankEntity.setFishCount4(fishCount4);
                            sqlMap.put("fishType",5);
                            int fishCount5 = activityPlayerMapper.selectCountByMap(sqlMap);
                            activityRankEntity.setFishCount5(fishCount5);
                            treeSet.add(activityRankEntity);
                        }
                        int index = 1;
                        for (ActivityRankEntity rankEntity : treeSet) {
                            rankEntity.setIndex(index);
                            activityRankMapper.insert(rankEntity);
                            index++;
                        }
                    }
                }
            }
        }
    }
    @Override
    public void test(){
        BaseProto.ErrorCode errorCode = null;
        long userId = 125L;
        long roleId = 1460868621033203089L;

//        ActivityProto.GetActivityListRequest request1 = ActivityProto.GetActivityListRequest.newBuilder()
//                .setUserId(userId).setRoleId(roleId).build();
//        ActivityProto.GetActivityListResponse.Builder response1 = ActivityProto.GetActivityListResponse.newBuilder();
//        errorCode = getActivityList(request1, response1);
//        log.info("response= {}", response1.toString());
//
//        ActivityProto.GetFishPoleListRequest request2 = ActivityProto.GetFishPoleListRequest.newBuilder()
//                .setUserId(userId).setRoleId(roleId).build();
//        ActivityProto.GetFishPoleListResponse.Builder response2 = ActivityProto.GetFishPoleListResponse.newBuilder();
//        errorCode = getFishPoleList(request2, response2);
//        log.info("response= {}", response2.toString());
//
//        ActivityProto.EnterActivityRequest request3 = ActivityProto.EnterActivityRequest.newBuilder()
//                .setUserId(userId).setRoleId(roleId).setActivityId(3L).build();
//        ActivityProto.EnterActivityResponse.Builder response3 = ActivityProto.EnterActivityResponse.newBuilder();
//        errorCode = enterActivity(request3, response3);
//        log.info("response= {}", response3.toString());
//
//        ActivityProto.FishingCastActionRequest request4 = ActivityProto.FishingCastActionRequest.newBuilder()
//                .setUserId(userId).setRoleId(roleId).setActivityId(3L).setPoleId(2).build();
//        ActivityProto.FishingCastActionResponse.Builder response4 = ActivityProto.FishingCastActionResponse.newBuilder();
//        errorCode = fishingCastAction(request4, response4);
//        log.info("response= {}", response4.toString());
//        log.info("response= {}", response4.toString());
//
//        ActivityProto.FishingTakeupActionRequest request5 = ActivityProto.FishingTakeupActionRequest.newBuilder()
//                .setUserId(userId).setRoleId(roleId).setActivityId(3L).setPoleId(2).build();
//        ActivityProto.FishingTakeupActionResponse.Builder response5 = ActivityProto.FishingTakeupActionResponse.newBuilder();
//        errorCode = fishingTakeUpAction(request5, response5);
//        log.info("response= {}", response5.toString());

//        ActivityProto.GetActivityRankRequest request6 = ActivityProto.GetActivityRankRequest.newBuilder()
//                .setUserId(userId).setRoleId(roleId).setActivityId(3L).build();
//        ActivityProto.GetActivityRankResponse.Builder response6 = ActivityProto.GetActivityRankResponse.newBuilder();
//        errorCode = getActivityRank(request6, response6);
//        log.info("response= {}", response6.toString());

//        ActivityProto.ExitActivityRequest request7 = ActivityProto.ExitActivityRequest.newBuilder()
//                .setUserId(userId).setRoleId(roleId).setActivityId(3L).build();
//        ActivityProto.ExitActivityResponse.Builder response7 = ActivityProto.ExitActivityResponse.newBuilder();
//        errorCode = exitActivity(request7, response7);
//        log.info("response= {}", response7.toString());
    }
}
